<?xml version="1.0" encoding="ascii"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>couchdb.design.ViewDefinition</title>
  <link rel="stylesheet" href="epydoc.css" type="text/css" />
  <script type="text/javascript" src="epydoc.js"></script>
</head>

<body bgcolor="white" text="black" link="blue" vlink="#204080"
      alink="#204080">
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="couchdb-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table width="100%" cellpadding="0" cellspacing="0">
  <tr valign="top">
    <td width="100%">
      <span class="breadcrumbs">
        <a href="couchdb-module.html">Package&nbsp;couchdb</a> ::
        <a href="couchdb.design-module.html">Module&nbsp;design</a> ::
        Class&nbsp;ViewDefinition
      </span>
    </td>
    <td>
      <table cellpadding="0" cellspacing="0">
        <!-- hide/show private -->
      </table>
    </td>
  </tr>
</table>
<!-- ==================== CLASS DESCRIPTION ==================== -->
<h1 class="epydoc">Class ViewDefinition</h1><p class="nomargin-top"></p>
<pre class="base-tree">
object --+
         |
        <strong class="uidshort">ViewDefinition</strong>
</pre>

<hr />
<p>Definition of a view stored in a specific design document.</p>
<p>An instance of this class can be used to access the results of the view,
as well as to keep the view definition in the design document up to date
with the definition in the application code.</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">from</span> couchdb <span class="py-keyword">import</span> Server
<span class="py-prompt">&gt;&gt;&gt; </span>server = Server(<span class="py-string">'http://localhost:5984/'</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>db = server.create(<span class="py-string">'python-tests'</span>)</pre>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>view = ViewDefinition(<span class="py-string">'tests'</span>, <span class="py-string">'all'</span>, <span class="py-string">'''function(doc) {</span>
<span class="py-more">... </span><span class="py-string">    emit(doc._id, null);</span>
<span class="py-more">... </span><span class="py-string">}'''</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>view.get_doc(db)</pre>
<p>The view is not yet stored in the database, in fact, design doc doesn't
even exist yet. That can be fixed using the <a href="couchdb.design.ViewDefinition-class.html#sync" class="link">sync</a> method:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>view.sync(db)</pre>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span>design_doc = view.get_doc(db)
<span class="py-prompt">&gt;&gt;&gt; </span>design_doc                                          <span class="py-comment">#doctest: +ELLIPSIS</span>
<span class="py-output">&lt;Document '_design/tests'@'...' {...}&gt;</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> design_doc[<span class="py-string">'views'</span>][<span class="py-string">'all'</span>][<span class="py-string">'map'</span>]
<span class="py-output">function(doc) {</span>
<span class="py-output">    emit(doc._id, null);</span>
<span class="py-output">}</span></pre>
<p>If you use a Python view server, you can also use Python functions instead
of code embedded in strings:</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">def</span> <span class="py-defname">my_map</span>(doc):
<span class="py-more">... </span>    yield doc[<span class="py-string">'somekey'</span>], doc[<span class="py-string">'somevalue'</span>]
<span class="py-prompt">&gt;&gt;&gt; </span>view = ViewDefinition(<span class="py-string">'test2'</span>, <span class="py-string">'somename'</span>, my_map, language=<span class="py-string">'python'</span>)
<span class="py-prompt">&gt;&gt;&gt; </span>view.sync(db)
<span class="py-prompt">&gt;&gt;&gt; </span>design_doc = view.get_doc(db)
<span class="py-prompt">&gt;&gt;&gt; </span>design_doc                                          <span class="py-comment">#doctest: +ELLIPSIS</span>
<span class="py-output">&lt;Document '_design/test2'@'...' {...}&gt;</span>
<span class="py-output"></span><span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">print</span> design_doc[<span class="py-string">'views'</span>][<span class="py-string">'somename'</span>][<span class="py-string">'map'</span>]
<span class="py-output">def my_map(doc):</span>
<span class="py-output">    yield doc['somekey'], doc['somevalue']</span></pre>
<p>Use the static <a href="couchdb.design.ViewDefinition-class.html#sync_many" class="link">sync_many()</a> method to create or update a collection of
views in the database in an atomic and efficient manner, even across
different design documents.</p>
<pre class="py-doctest">
<span class="py-prompt">&gt;&gt;&gt; </span><span class="py-keyword">del</span> server[<span class="py-string">'python-tests'</span>]</pre>

<!-- ==================== INSTANCE METHODS ==================== -->
<a name="section-InstanceMethods"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Instance Methods</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#__init__" class="summary-sig-name">__init__</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">design</span>,
        <span class="summary-sig-arg">name</span>,
        <span class="summary-sig-arg">map_fun</span>,
        <span class="summary-sig-arg">reduce_fun</span>=<span class="summary-sig-default">None</span>,
        <span class="summary-sig-arg">language</span>=<span class="summary-sig-default"><code class="variable-quote">'</code><code class="variable-string">javascript</code><code class="variable-quote">'</code></span>,
        <span class="summary-sig-arg">wrapper</span>=<span class="summary-sig-default">None</span>,
        <span class="summary-sig-arg">**defaults</span>)</span><br />
      Initialize the view definition.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type"><a href="couchdb.client.ViewResults-class.html" class="link">ViewResults</a></span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#__call__" class="summary-sig-name">__call__</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">db</span>,
        <span class="summary-sig-arg">**options</span>)</span><br />
      Execute the view in the given database.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#__repr__" class="summary-sig-name">__repr__</a>(<span class="summary-sig-arg">self</span>)</span><br />
      repr(x)</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type"><a href="couchdb.client.Document-class.html" class="link">Document</a></span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#get_doc" class="summary-sig-name">get_doc</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">db</span>)</span><br />
      Retrieve and return the design document corresponding to this view
definition from the given database.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#sync" class="summary-sig-name">sync</a>(<span class="summary-sig-arg">self</span>,
        <span class="summary-sig-arg">db</span>)</span><br />
      Ensure that the view stored in the database matches the view defined
by this instance.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__delattr__</code>,
      <code>__format__</code>,
      <code>__getattribute__</code>,
      <code>__hash__</code>,
      <code>__new__</code>,
      <code>__reduce__</code>,
      <code>__reduce_ex__</code>,
      <code>__setattr__</code>,
      <code>__sizeof__</code>,
      <code>__str__</code>,
      <code>__subclasshook__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== STATIC METHODS ==================== -->
<a name="section-StaticMethods"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Static Methods</span></td>
</tr>
<tr>
    <td width="15%" align="right" valign="top" class="summary">
      <span class="summary-type">&nbsp;</span>
    </td><td class="summary">
      <table width="100%" cellpadding="0" cellspacing="0" border="0">
        <tr>
          <td><span class="summary-sig"><a href="couchdb.design.ViewDefinition-class.html#sync_many" class="summary-sig-name">sync_many</a>(<span class="summary-sig-arg">db</span>,
        <span class="summary-sig-arg">views</span>,
        <span class="summary-sig-arg">remove_missing</span>=<span class="summary-sig-default">False</span>,
        <span class="summary-sig-arg">callback</span>=<span class="summary-sig-default">None</span>)</span><br />
      Ensure that the views stored in the database that correspond to a
given list of <a href="couchdb.design.ViewDefinition-class.html" class="link">ViewDefinition</a> instances match the code defined in
those instances.</td>
          <td align="right" valign="top">
            
            
          </td>
        </tr>
      </table>
      
    </td>
  </tr>
</table>
<!-- ==================== PROPERTIES ==================== -->
<a name="section-Properties"></a>
<table class="summary" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Properties</span></td>
</tr>
  <tr>
    <td colspan="2" class="summary">
    <p class="indent-wrapped-lines"><b>Inherited from <code>object</code></b>:
      <code>__class__</code>
      </p>
    </td>
  </tr>
</table>
<!-- ==================== METHOD DETAILS ==================== -->
<a name="section-MethodDetails"></a>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr bgcolor="#70b0f0" class="table-header">
  <td align="left" colspan="2" class="table-header">
    <span class="table-header">Method Details</span></td>
</tr>
</table>
<a name="__init__"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">__init__</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">design</span>,
        <span class="sig-arg">name</span>,
        <span class="sig-arg">map_fun</span>,
        <span class="sig-arg">reduce_fun</span>=<span class="sig-default">None</span>,
        <span class="sig-arg">language</span>=<span class="sig-default"><code class="variable-quote">'</code><code class="variable-string">javascript</code><code class="variable-quote">'</code></span>,
        <span class="sig-arg">wrapper</span>=<span class="sig-default">None</span>,
        <span class="sig-arg">**defaults</span>)</span>
    <br /><em class="fname">(Constructor)</em>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  <p>Initialize the view definition.</p>
<p>Note that the code in <code class="link">map_fun</code> and <code class="link">reduce_fun</code> is automatically
dedented, that is, any common leading whitespace is removed from each
line.</p>
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>design</code></strong> - the name of the design document</li>
        <li><strong class="pname"><code>name</code></strong> - the name of the view</li>
        <li><strong class="pname"><code>map_fun</code></strong> - the map function code</li>
        <li><strong class="pname"><code>reduce_fun</code></strong> - the reduce function code (optional)</li>
        <li><strong class="pname"><code>language</code></strong> - the name of the language used</li>
        <li><strong class="pname"><code>wrapper</code></strong> - an optional callable that should be used to wrap the
result rows</li>
    </ul></dd>
    <dt>Overrides:
        object.__init__
    </dt>
  </dl>
</td></tr></table>
</div>
<a name="__call__"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">__call__</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">db</span>,
        <span class="sig-arg">**options</span>)</span>
    <br /><em class="fname">(Call operator)</em>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  Execute the view in the given database.
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>db</code></strong> - the <a href="couchdb.client.Database-class.html" class="link">Database</a> instance</li>
        <li><strong class="pname"><code>options</code></strong> - optional query string parameters</li>
    </ul></dd>
    <dt>Returns: <a href="couchdb.client.ViewResults-class.html" class="link">ViewResults</a></dt>
        <dd>the view results</dd>
  </dl>
</td></tr></table>
</div>
<a name="__repr__"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">__repr__</span>(<span class="sig-arg">self</span>)</span>
    <br /><em class="fname">(Representation operator)</em>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  repr(x)
  <dl class="fields">
    <dt>Overrides:
        object.__repr__
        <dd><em class="note">(inherited documentation)</em></dd>
    </dt>
  </dl>
</td></tr></table>
</div>
<a name="get_doc"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">get_doc</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">db</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  Retrieve and return the design document corresponding to this view
definition from the given database.
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>db</code></strong> - the <a href="couchdb.client.Database-class.html" class="link">Database</a> instance</li>
    </ul></dd>
    <dt>Returns: <a href="couchdb.client.Document-class.html" class="link">Document</a></dt>
        <dd>a <a href="couchdb.client.Document-class.html" class="link">client.Document</a> instance, or <code class="link">None</code> if the design document
does not exist in the database</dd>
  </dl>
</td></tr></table>
</div>
<a name="sync"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">sync</span>(<span class="sig-arg">self</span>,
        <span class="sig-arg">db</span>)</span>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  Ensure that the view stored in the database matches the view defined
by this instance.
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>db</code></strong> - the <a href="couchdb.client.Database-class.html" class="link">Database</a> instance</li>
    </ul></dd>
  </dl>
</td></tr></table>
</div>
<a name="sync_many"></a>
<div>
<table class="details" border="1" cellpadding="3"
       cellspacing="0" width="100%" bgcolor="white">
<tr><td>
  <table width="100%" cellpadding="0" cellspacing="0" border="0">
  <tr valign="top"><td>
  <h3 class="epydoc"><span class="sig"><span class="sig-name">sync_many</span>(<span class="sig-arg">db</span>,
        <span class="sig-arg">views</span>,
        <span class="sig-arg">remove_missing</span>=<span class="sig-default">False</span>,
        <span class="sig-arg">callback</span>=<span class="sig-default">None</span>)</span>
    <br /><em class="fname">Static Method</em>
  </h3>
  </td><td align="right" valign="top"
    >&nbsp;
    </td>
  </tr></table>
  
  <p>Ensure that the views stored in the database that correspond to a
given list of <a href="couchdb.design.ViewDefinition-class.html" class="link">ViewDefinition</a> instances match the code defined in
those instances.</p>
<p>This function might update more than one design document. This is done
using the CouchDB bulk update feature to ensure atomicity of the
operation.</p>
  <dl class="fields">
    <dt>Parameters:</dt>
    <dd><ul class="nomargin-top">
        <li><strong class="pname"><code>db</code></strong> - the <a href="couchdb.client.Database-class.html" class="link">Database</a> instance</li>
        <li><strong class="pname"><code>views</code></strong> - a sequence of <a href="couchdb.design.ViewDefinition-class.html" class="link">ViewDefinition</a> instances</li>
        <li><strong class="pname"><code>remove_missing</code></strong> - whether views found in a design document that
are not found in the list of <a href="couchdb.design.ViewDefinition-class.html" class="link">ViewDefinition</a>
instances should be removed</li>
        <li><strong class="pname"><code>callback</code></strong> - a callback function that is invoked when a design
document gets updated; the callback gets passed the
design document as only parameter, before that doc
has actually been saved back to the database</li>
    </ul></dd>
  </dl>
</td></tr></table>
</div>
<br />
<!-- ==================== NAVIGATION BAR ==================== -->
<table class="navbar" border="0" width="100%" cellpadding="0"
       bgcolor="#a0c0ff" cellspacing="0">
  <tr valign="middle">
  <!-- Home link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="couchdb-module.html">Home</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Tree link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="module-tree.html">Trees</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Index link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="identifier-index.html">Indices</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Help link -->
      <th>&nbsp;&nbsp;&nbsp;<a
        href="help.html">Help</a>&nbsp;&nbsp;&nbsp;</th>

  <!-- Project homepage -->
      <th class="navbar" align="right" width="100%">
        <table border="0" cellpadding="0" cellspacing="0">
          <tr><th class="navbar" align="center"
            ><a class="navbar" target="_top" href="../index.html">Documentation Index</a></th>
          </tr></table></th>
  </tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%%">
  <tr>
    <td align="left" class="footer">
    Generated by Epydoc 3.0.1 on Mon Dec 14 15:25:33 2009
    </td>
    <td align="right" class="footer">
      <a target="mainFrame" href="http://epydoc.sourceforge.net"
        >http://epydoc.sourceforge.net</a>
    </td>
  </tr>
</table>

<script type="text/javascript">
  <!--
  // Private objects are initially displayed (because if
  // javascript is turned off then we want them to be
  // visible); but by default, we want to hide them.  So hide
  // them unless we have a cookie that says to show them.
  checkCookie();
  // -->
</script>
</body>
</html>
